<template>
  <div class="my-comments">
    <!-- 页面标题 -->
    <div class="page-header">
      <button class="back-button" @click="goBack">
        <i class="icon-back"></i>
      </button>
      <h1 class="page-title">我的评论</h1>
      <div class="placeholder"></div>
    </div>

    <!-- 加载状态 -->
    <div v-if="loading" class="loading-container">
      <div class="loading-spinner"></div>
      <p>加载中...</p>
    </div>

    <!-- 空状态 -->
    <div v-else-if="comments.length === 0" class="empty-state">
      <div class="empty-icon">💬</div>
      <p class="empty-text">还没有评论</p>
      <p class="empty-hint">快去发表你的第一条评论吧</p>
    </div>

    <!-- 评论列表 -->
    <div v-else class="comments-list">
      <div 
        v-for="comment in comments" 
        :key="comment.id" 
        class="comment-item"
        @click="goToPost(comment)"
      >
        <div class="comment-header">
          <span class="comment-time">{{ formatTime(comment.createTime) }}</span>
          <span class="comment-type" :class="comment.parentId ? 'reply' : 'comment'">
            {{ comment.parentId ? '回复' : '评论' }}
          </span>
        </div>
        
        <div class="comment-content">
          {{ comment.content }}
        </div>

        <div v-if="comment.parentContent" class="parent-comment">
          <span class="parent-label">回复:</span>
          <span class="parent-text">{{ comment.parentContent }}</span>
        </div>

        <div class="comment-footer">
          <div class="post-info">
            <span class="post-title">{{ comment.postTitle || '帖子详情' }}</span>
          </div>
          <div class="comment-actions">
            <span class="stat-item">
              <i class="icon-like"></i>
              {{ comment.likeCount || 0 }}
            </span>
            <button class="delete-btn" @click.stop="deleteComment(comment)">
              删除
            </button>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import { useRouter } from 'vue-router'
import { useUserStore } from '@/stores/user'
import { getMyComments, deleteForumComment } from '@/api/forum'

const router = useRouter()
const userStore = useUserStore()

const loading = ref(true)
const comments = ref([])

// 返回上一页
const goBack = () => {
  router.back()
}

// 格式化时间
const formatTime = (timeString) => {
  if (!timeString) return ''
  
  const date = new Date(timeString)
  const now = new Date()
  const diff = now - date
  
  // 小于1分钟
  if (diff < 60000) {
    return '刚刚'
  }
  // 小于1小时
  if (diff < 3600000) {
    return `${Math.floor(diff / 60000)}分钟前`
  }
  // 小于1天
  if (diff < 86400000) {
    return `${Math.floor(diff / 3600000)}小时前`
  }
  // 小于7天
  if (diff < 604800000) {
    return `${Math.floor(diff / 86400000)}天前`
  }
  
  // 超过7天显示具体日期
  return date.toLocaleDateString('zh-CN', {
    year: 'numeric',
    month: '2-digit',
    day: '2-digit'
  })
}

// 跳转到帖子详情
const goToPost = (comment) => {
  if (comment.postId) {
    router.push(`/forum/post/${comment.postId}`)
  }
}

// 获取评论列表
const fetchComments = async () => {
  if (!userStore.uid) {
    console.error('用户未登录')
    router.push('/user/login')
    return
  }

  loading.value = true
  try {
    const res = await getMyComments(userStore.uid)
    if (res.code === 200) {
      comments.value = res.data || []
    } else {
      console.error('获取评论列表失败:', res.msg)
    }
  } catch (error) {
    console.error('获取评论列表失败:', error)
  } finally {
    loading.value = false
  }
}

// 删除评论
const deleteComment = async (comment) => {
  if (!confirm('确定要删除这条评论吗？')) {
    return
  }

  try {
    const res = await deleteForumComment(comment.id, userStore.uid)
    console.log('删除评论成功:', res)

    if (res.code === 200) {
      // 删除成功后，重新获取评论列表
      await fetchComments()
      alert('评论已删除')
    } else {
      alert('删除失败：' + (res.message || res.msg || '未知错误'))
    }
  } catch (error) {
    console.error('删除评论失败:', error)
    alert('删除评论失败，请稍后重试')
  }
}

onMounted(() => {
  fetchComments()
})
</script>

<style scoped>
.my-comments {
  min-height: 100vh;
  background: #f5f7fa;
}

/* 页面标题 */
.page-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 16px 20px;
  background: white;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.05);
  position: sticky;
  top: 0;
  z-index: 100;
}

.back-button {
  width: 36px;
  height: 36px;
  border: none;
  background: transparent;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: 8px;
  transition: all 0.3s ease;
}

.back-button:hover {
  background: #f0f2f5;
}

.icon-back::before {
  content: '←';
  font-size: 1.5rem;
  color: #1976d2;
}

.page-title {
  font-size: 1.2rem;
  font-weight: 600;
  color: #1f2937;
  margin: 0;
}

.placeholder {
  width: 36px;
}

/* 加载状态 */
.loading-container {
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  height: 300px;
  color: #666;
}

.loading-spinner {
  width: 40px;
  height: 40px;
  border: 3px solid #f3f3f3;
  border-top: 3px solid #1976d2;
  border-radius: 50%;
  animation: spin 1s linear infinite;
  margin-bottom: 16px;
}

@keyframes spin {
  0% { transform: rotate(0deg); }
  100% { transform: rotate(360deg); }
}

/* 空状态 */
.empty-state {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 80px 20px;
  color: #9ca3af;
}

.empty-icon {
  font-size: 4rem;
  margin-bottom: 16px;
  opacity: 0.5;
}

.empty-text {
  font-size: 1.1rem;
  font-weight: 500;
  margin: 0 0 8px 0;
  color: #6b7280;
}

.empty-hint {
  font-size: 0.9rem;
  margin: 0;
}

/* 评论列表 */
.comments-list {
  padding: 16px;
}

.comment-item {
  background: white;
  border-radius: 12px;
  padding: 16px;
  margin-bottom: 12px;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.05);
  cursor: pointer;
  transition: all 0.3s ease;
}

.comment-item:hover {
  box-shadow: 0 4px 12px rgba(25, 118, 210, 0.15);
  transform: translateY(-2px);
}

.comment-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 12px;
}

.comment-time {
  font-size: 0.85rem;
  color: #9ca3af;
}

.comment-type {
  padding: 4px 12px;
  border-radius: 12px;
  font-size: 0.8rem;
  font-weight: 500;
}

.comment-type.comment {
  background: linear-gradient(135deg, #e3f2fd, #bbdefb);
  color: #1976d2;
}

.comment-type.reply {
  background: linear-gradient(135deg, #f3e5f5, #e1bee7);
  color: #7b1fa2;
}

.comment-content {
  font-size: 0.95rem;
  line-height: 1.6;
  color: #1f2937;
  margin-bottom: 12px;
}

.parent-comment {
  background: #f9fafb;
  border-left: 3px solid #1976d2;
  padding: 8px 12px;
  margin-bottom: 12px;
  border-radius: 4px;
  font-size: 0.85rem;
}

.parent-label {
  color: #1976d2;
  font-weight: 500;
  margin-right: 4px;
}

.parent-text {
  color: #6b7280;
}

.comment-footer {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding-top: 12px;
  border-top: 1px solid #f0f2f5;
}

.post-info {
  flex: 1;
  overflow: hidden;
}

.post-title {
  font-size: 0.85rem;
  color: #6b7280;
  display: block;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.comment-actions {
  display: flex;
  align-items: center;
  gap: 16px;
}

.stat-item {
  display: flex;
  align-items: center;
  gap: 4px;
  font-size: 0.85rem;
  color: #9ca3af;
}

.delete-btn {
  background: none;
  border: none;
  color: #ff4757;
  cursor: pointer;
  font-size: 0.85rem;
  padding: 4px 8px;
  border-radius: 4px;
  transition: all 0.3s ease;
}

.delete-btn:hover {
  background: #fff5f5;
  color: #d63031;
}

.icon-like::before {
  content: '👍';
  font-size: 0.9rem;
}

/* 移动端适配 */
@media (max-width: 768px) {
  .page-header {
    padding: 12px 16px;
  }

  .page-title {
    font-size: 1.1rem;
  }

  .comments-list {
    padding: 12px;
  }

  .comment-item {
    padding: 14px;
  }
}
</style>
